﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;

namespace YueRu.Common.Xml
{
    public class XmlHelper
    {
        public static string CreateResponse(string url, IDictionary<string, string> parameters)
        {
            string xmlurl = "";
            HttpWebResponse webResponse = null;
            try
            {
                webResponse = HttpWebResponseHelper.CreatePostHttpResponse(url, parameters, 10000000, null, Encoding.UTF8, null);
                Stream streamReceive = webResponse.GetResponseStream();
                StreamReader streamReader = new StreamReader(streamReceive, Encoding.UTF8);
                xmlurl = streamReader.ReadToEnd();
            }
            finally
            {
                if (webResponse != null) webResponse.Close();
            }
            return xmlurl;
        }

        public static string GetPic(string url, IDictionary<string, string> parameters, string SerialBrandId)
        {
            string imgurl = "";
            try
            {
                string result = CreateResponse(url, parameters);
                imgurl = GetImgSrc(result, SerialBrandId);
            }
            catch
            {
                imgurl = "";
            }
            return imgurl;
        }

        public static string GetImgSrc(string xmlStr, string SerialBrandId)
        {
            byte[] encodedString = Encoding.UTF8.GetBytes(xmlStr);
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(encodedString))
            {
                ms.Flush();
                ms.Position = 0;
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(ms);
                string xpath = @"WhiteCoverImages/SerialBrand[@ID='" + SerialBrandId + "']/CarYear";
                if (SerialBrandId == "2583")
                {
                    xpath = @"WhiteCoverImages/SerialBrand[@ID='" + SerialBrandId + "']/CarYear[@Year=2012]";
                }
                string ImageUrl = xmlDoc.SelectSingleNode(xpath).Attributes["ImageUrl"].Value;
                return ImageUrl;
            }
        }

        public static string GetImgSrc(string xmlStr, ArrayList list)
        {
            byte[] encodedString = Encoding.UTF8.GetBytes(xmlStr);
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(encodedString))
            {
                ms.Flush();
                ms.Position = 0;
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(ms);
                string ImageUrl = "";
                for (int i = 0; i < list.Count; i++)
                {
                    string xpath = @"WhiteCoverImages/SerialBrand[@ID='" + list[i] + "']/CarYear";
                    if ((string)list[i] == "2583")
                    {
                        xpath = @"WhiteCoverImages/SerialBrand[@ID='" + list[i] + "']/CarYear[@Year=2012]";
                    }
                    string imgs = xmlDoc.SelectSingleNode(xpath).Attributes["ImageUrl"].Value;
                    ImageUrl += ",{name:'TeMaiA" + list[i] + "',value:'" + imgs + "'}";
                }
                return ImageUrl;
            }
        }
    }
}